<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Huffman coding</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Huffman_coding"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Huffman_coding rootpage-Huffman_coding skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Huffman coding</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Not to be confused with <a href="Hamming_code" title="Hamming code">Hamming code</a>.</div>
<p class="mw-empty-elt">
</p>
<p>In <a href="Computer_science" title="Computer science">computer science</a> and <a href="Information_theory" title="Information theory">information theory</a>, a <b>Huffman code</b> is a particular type of optimal <a href="Prefix_code" title="Prefix code">prefix code</a> that is commonly used for <a href="Lossless_data_compression" class="mw-redirect" title="Lossless data compression">lossless data compression</a>. The process of finding or using such a code is <b>Huffman coding</b>, an algorithm developed by <a href="David_A._Huffman" title="David A. Huffman">David A. Huffman</a> while he was a <a href="Doctor_of_Science" title="Doctor of Science">Sc.D.</a> student at <a href="Massachusetts_Institute_of_Technology" title="Massachusetts Institute of Technology">MIT</a>, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".<sup id="cite_ref-:0_1-0" class="reference"><a href="#cite_note-:0-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p><p>The output from Huffman's algorithm can be viewed as a <a href="Variable-length_code" title="Variable-length code">variable-length code</a> table for encoding a source symbol (such as a character in a file). The algorithm derives this table from the estimated probability or frequency of occurrence (<i>weight</i>) for each possible value of the source symbol. As in other <a href="Entropy_encoding" class="mw-redirect" title="Entropy encoding">entropy encoding</a> methods, more common symbols are generally represented using fewer bits than less common symbols. Huffman's method can be efficiently implemented, finding a code in time <a href="Linear_time" class="mw-redirect" title="Linear time">linear</a> to the number of input weights if these weights are sorted.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> However, although optimal among methods encoding symbols separately, Huffman coding <a href="#Optimality">is not always optimal</a> among all compression methods – it is replaced with <a href="Arithmetic_coding" title="Arithmetic coding">arithmetic coding</a><sup id="cite_ref-LiDrew2014_3-0" class="reference"><a href="#cite_note-LiDrew2014-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> or <a href="Asymmetric_numeral_systems" title="Asymmetric numeral systems">asymmetric numeral systems</a><sup id="cite_ref-PCS2015_4-0" class="reference"><a href="#cite_note-PCS2015-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> if a better compression ratio is required.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<p>In 1951, <a href="David_A._Huffman" title="David A. Huffman">David A. Huffman</a> and his <a href="MIT" class="mw-redirect" title="MIT">MIT</a> <a href="Information_theory" title="Information theory">information theory</a> classmates were given the choice of a term paper or a final <a href="Exam" title="Exam">exam</a>. The professor, <a href="Robert_M._Fano" class="mw-redirect" title="Robert M. Fano">Robert M. Fano</a>, assigned a <a href="Term_paper" title="Term paper">term paper</a> on the problem of finding the most efficient binary code. Huffman, unable to prove any codes were the most efficient, was about to give up and start studying for the final when he hit upon the idea of using a frequency-sorted <a href="Binary_tree" title="Binary tree">binary tree</a> and quickly proved this method the most efficient.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p><p>In doing so, Huffman outdid Fano, who had worked with <a href="Claude_Shannon" title="Claude Shannon">Claude Shannon</a> to develop a similar code. Building the tree from the bottom up guaranteed optimality, unlike the top-down approach of <a href="Shannon%E2%80%93Fano_coding" title="Shannon–Fano coding">Shannon–Fano coding</a>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Terminology">Terminology</h2></div>
<p>Huffman coding uses a specific method for choosing the representation for each symbol, resulting in a <a href="Prefix_code" title="Prefix code">prefix code</a> (sometimes called "prefix-free codes", that is, the bit string representing some particular symbol is never a prefix of the bit string representing any other symbol). Huffman coding is such a widespread method for creating prefix codes that the term "Huffman code" is widely used as a synonym for "prefix code" even when such a code is not produced by Huffman's algorithm.
</p>
<div class="mw-heading mw-heading2"><h2 id="Problem_definition">Problem definition</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1305420660">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}html body.mediawiki .mw-parser-output .metadata.ambox{border-left:10px solid #36c!important;background-color:var(--background-color-neutral-subtle,#fbfbfb)!important}html body.mediawiki .mw-parser-output .metadata.ambox-speedy{border-left:10px solid #b32424!important;background-color:var(--background-color-destructive-subtle,#fee7e6)!important}html body.mediawiki .mw-parser-output .metadata.ambox-delete{border-left:10px solid #b32424!important}html body.mediawiki .mw-parser-output .metadata.ambox-content{border-left:10px solid #f28500!important}html body.mediawiki .mw-parser-output .metadata.ambox-style{border-left:10px solid #fc3!important}html body.mediawiki .mw-parser-output .metadata.ambox-move{border-left:10px solid #9932cc!important}html body.mediawiki .mw-parser-output .metadata.ambox-protection{border-left:10px solid #a2a9b1!important}.mw-parser-output .ambox.mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style>
<div class="mw-heading mw-heading3"><h3 id="Informal_description">Informal description</h3></div>
<dl><dt>Given</dt>
<dd>A set of symbols <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle S}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>S</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle S}</annotation>
</semantics>
</math></span><img src="./4611d85173cd3b508e67077d4a1252c9c05abca2.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.499ex; height:2.176ex;" alt="{\displaystyle S}" loading="lazy"></span> and for each symbol <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x\in S}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
<mo>∈<!-- ∈ --></mo>
<mi>S</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x\in S}</annotation>
</semantics>
</math></span><img src="./51186ba8afb2067573a9082d55dd383df1ea9214.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:5.67ex; height:2.176ex;" alt="{\displaystyle x\in S}" loading="lazy"></span>, the frequency <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle f_{x}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>f</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>x</mi>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle f_{x}}</annotation>
</semantics>
</math></span><img src="./bb36605624ab2287dc5ec558513c625b88acfee3.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:2.312ex; height:2.509ex;" alt="{\displaystyle f_{x}}" loading="lazy"></span> representing the fraction of symbols in the text that are equal to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x}</annotation>
</semantics>
</math></span><img src="./87f9e315fd7e2ba406057a97300593c4802b53e4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}" loading="lazy"></span>.<sup id="cite_ref-kleinberg&Tardos_6-0" class="reference"><a href="#cite_note-kleinberg&Tardos-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup></dd>
<dt>Find</dt>
<dd>A <a href="Prefix_code" title="Prefix code">prefix-free binary code</a> (a set of codewords) with minimum <a href="Expected_value" title="Expected value">expected</a> codeword length (equivalently, a tree with minimum weighted path length from the root).</dd></dl>
<div class="mw-heading mw-heading3"><h3 id="Formalized_description">Formalized description</h3></div>
<p><b>Input</b>.<br>
Alphabet <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle A=(a_{1},a_{2},\dots ,a_{n})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>A</mi>
<mo>=</mo>
<mo stretchy="false">(</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle A=(a_{1},a_{2},\dots ,a_{n})}</annotation>
</semantics>
</math></span><img src="./cd7971cd308b54a9afb4b897a990d32c3c5174f4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:19.879ex; height:2.843ex;" alt="{\displaystyle A=(a_{1},a_{2},\dots ,a_{n})}" loading="lazy"></span>, which is the symbol alphabet of size <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n}</annotation>
</semantics>
</math></span><img src="./a601995d55609f2d9f5e233e36fbe9ea26011b3b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.395ex; height:1.676ex;" alt="{\displaystyle n}" loading="lazy"></span>. <br>
Tuple <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle W=(w_{1},w_{2},\dots ,w_{n})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>W</mi>
<mo>=</mo>
<mo stretchy="false">(</mo>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle W=(w_{1},w_{2},\dots ,w_{n})}</annotation>
</semantics>
</math></span><img src="./eea6a64cad1424b3d68c8ac38a360c5383486ce4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:21.875ex; height:2.843ex;" alt="{\displaystyle W=(w_{1},w_{2},\dots ,w_{n})}" loading="lazy"></span>, which is the tuple of the (positive) symbol weights (usually proportional to probabilities), i.e. <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle w_{i}=\operatorname {weight} \left(a_{i}\right),\,i\in \{1,2,\dots ,n\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>=</mo>
<mi>weight</mi>
<mo><!-- --></mo>
<mrow>
<mo>(</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>)</mo>
</mrow>
<mo>,</mo>
<mspace width="thinmathspace"></mspace>
<mi>i</mi>
<mo>∈<!-- ∈ --></mo>
<mo fence="false" stretchy="false">{</mo>
<mn>1</mn>
<mo>,</mo>
<mn>2</mn>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<mi>n</mi>
<mo fence="false" stretchy="false">}</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle w_{i}=\operatorname {weight} \left(a_{i}\right),\,i\in \{1,2,\dots ,n\}}</annotation>
</semantics>
</math></span><img src="./5d928718b91ee67e2ffe43864cdd8a4e9e815007.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:33.439ex; height:2.843ex;" alt="{\displaystyle w_{i}=\operatorname {weight} \left(a_{i}\right),\,i\in \{1,2,\dots ,n\}}" loading="lazy"></span>. <br>
<br>
<b>Output</b>.<br>
Code <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle C\left(W\right)=(c_{1},c_{2},\dots ,c_{n})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>C</mi>
<mrow>
<mo>(</mo>
<mi>W</mi>
<mo>)</mo>
</mrow>
<mo>=</mo>
<mo stretchy="false">(</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle C\left(W\right)=(c_{1},c_{2},\dots ,c_{n})}</annotation>
</semantics>
</math></span><img src="./ea3b6a7da9769c2d5f87be6ea75b14ea4ee97621.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:23.865ex; height:2.843ex;" alt="{\displaystyle C\left(W\right)=(c_{1},c_{2},\dots ,c_{n})}" loading="lazy"></span>, which is the tuple of (binary) codewords, where <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle c_{i}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle c_{i}}</annotation>
</semantics>
</math></span><img src="./01acb7953ba52c2aa44264b5d0f8fd223aa178a2.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:1.807ex; height:2.009ex;" alt="{\displaystyle c_{i}}" loading="lazy"></span> is the codeword for <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle a_{i},\,i\in \{1,2,\dots ,n\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>,</mo>
<mspace width="thinmathspace"></mspace>
<mi>i</mi>
<mo>∈<!-- ∈ --></mo>
<mo fence="false" stretchy="false">{</mo>
<mn>1</mn>
<mo>,</mo>
<mn>2</mn>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<mi>n</mi>
<mo fence="false" stretchy="false">}</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle a_{i},\,i\in \{1,2,\dots ,n\}}</annotation>
</semantics>
</math></span><img src="./447bbca06d834d18d87bd270b4eadede332990bb.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:19.35ex; height:2.843ex;" alt="{\displaystyle a_{i},\,i\in \{1,2,\dots ,n\}}" loading="lazy"></span>.<br>
<br>
<b>Goal</b>.<br>
Let <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\textstyle L(C(W))=\sum _{i=1}^{n}w_{i}\operatorname {length} (c_{i})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="false" scriptlevel="0">
<mi>L</mi>
<mo stretchy="false">(</mo>
<mi>C</mi>
<mo stretchy="false">(</mo>
<mi>W</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">)</mo>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
<mo>=</mo>
<mn>1</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</munderover>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mi>length</mi>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\textstyle L(C(W))=\sum _{i=1}^{n}w_{i}\operatorname {length} (c_{i})}</annotation>
</semantics>
</math></span><img src="./c450f9a883d163b16ded56056d9b0c5f720874f6.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.005ex; width:31.041ex; height:3.176ex;" alt="{\textstyle L(C(W))=\sum _{i=1}^{n}w_{i}\operatorname {length} (c_{i})}" loading="lazy"></span> be the weighted path length of code <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle C}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>C</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle C}</annotation>
</semantics>
</math></span><img src="./4fc55753007cd3c18576f7933f6f089196732029.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.766ex; height:2.176ex;" alt="{\displaystyle C}" loading="lazy"></span>. Condition: <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle L(C(W))\leq L(T(W))}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>L</mi>
<mo stretchy="false">(</mo>
<mi>C</mi>
<mo stretchy="false">(</mo>
<mi>W</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">)</mo>
<mo>≤<!-- ≤ --></mo>
<mi>L</mi>
<mo stretchy="false">(</mo>
<mi>T</mi>
<mo stretchy="false">(</mo>
<mi>W</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle L(C(W))\leq L(T(W))}</annotation>
</semantics>
</math></span><img src="./495b97b04a92245e7a8b3fc3eeda5b3b6fff6ef8.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:21.774ex; height:2.843ex;" alt="{\displaystyle L(C(W))\leq L(T(W))}" loading="lazy"></span> for any code <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T(W)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
<mo stretchy="false">(</mo>
<mi>W</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T(W)}</annotation>
</semantics>
</math></span><img src="./12bbad443fb07fbf7882a2a4af08d919952cb4c9.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.881ex; height:2.843ex;" alt="{\displaystyle T(W)}" loading="lazy"></span>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Example">Example</h3></div>
<p>We give an example of the result of Huffman coding for a code with five characters and given weights. We will not verify that it minimizes <i>L</i> over all codes, but we will compute <i>L</i> and compare it to the <a href="Shannon_entropy" class="mw-redirect" title="Shannon entropy">Shannon entropy</a> <i>H</i> of the given set of weights; the result is nearly optimal.
</p>
<table class="wikitable">
<tbody><tr>
<th rowspan="2" style="background:#efefef">Input (<i>A</i>, <i>W</i>)
</th>
<th style="background:#efefef;font-weight:normal">Symbol (<span class="texhtml"><i>a</i><sub><i>i</i></sub></span>)
</th>
<td align="center" style="background:#efefef">a
</td>
<td align="center" style="background:#efefef">b
</td>
<td align="center" style="background:#efefef">c
</td>
<td align="center" style="background:#efefef">d
</td>
<td align="center" style="background:#efefef">e
</td>
<th style="background:#efefef">Sum
</th></tr>
<tr>
<th style="background:#efefef;font-weight:normal">Weights (<span class="texhtml"><i>w</i><sub><i>i</i></sub></span>)
</th>
<td align="center">0.10
</td>
<td align="center">0.15
</td>
<td align="center">0.30
</td>
<td align="center">0.16
</td>
<td align="center">0.29
</td>
<td align="center">= 1
</td></tr>
<tr>
<th rowspan="3" style="background:#efefef">Output <i>C</i>
</th>
<th style="background:#efefef;font-weight:normal">Codewords (<span class="texhtml"><i>c</i><sub><i>i</i></sub></span>)
</th>
<td align="center"><code>010</code>
</td>
<td align="center"><code>011</code>
</td>
<td align="center"><code>11</code>
</td>
<td align="center"><code>00</code>
</td>
<td align="center"><code>10</code>
</td>
<td rowspan="2">
</td></tr>
<tr>
<th style="background:#efefef;font-weight:normal">Codeword length (in bits)<br>(<span class="texhtml"><i>ℓ</i><sub><i>i</i></sub></span>)
</th>
<td align="center">3
</td>
<td align="center">3
</td>
<td align="center">2
</td>
<td align="center">2
</td>
<td align="center">2
</td></tr>
<tr>
<th style="background:#efefef;font-weight:normal">Contribution to weighted path length<br>(<span class="texhtml"><i>ℓ</i><sub><i>i</i></sub></span> <span class="texhtml"><i>w</i><sub><i>i</i></sub></span> )
</th>
<td align="center">0.30
</td>
<td align="center">0.45
</td>
<td align="center">0.60
</td>
<td align="center">0.32
</td>
<td align="center">0.58
</td>
<td align="center"><i>L</i>(<i>C</i>) = 2.25
</td></tr>
<tr>
<th rowspan="3" style="background:#efefef">Optimality
</th>
<th style="background:#efefef;font-weight:normal">Probability budget<br>(<span class="texhtml">2<sup>−<i>ℓ</i><sub><i>i</i></sub></sup></span>)
</th>
<td align="center">1/8
</td>
<td align="center">1/8
</td>
<td align="center">1/4
</td>
<td align="center">1/4
</td>
<td align="center">1/4
</td>
<td align="center">= 1.00
</td></tr>
<tr>
<th style="background: #efefef; font-weight: normal;">Information content (in bits)<br>(<span class="texhtml">−log<sub>2</sub> <i>w</i><sub><i>i</i></sub></span>) ≈
</th>
<td align="center">3.32
</td>
<td align="center">2.74
</td>
<td align="center">1.74
</td>
<td align="center">2.64
</td>
<td align="center">1.79
</td>
<td align="center">
</td></tr>
<tr>
<th style="background: #efefef; font-weight: normal;">Contribution to entropy<br>(<span class="texhtml">−<i>w</i><sub><i>i</i></sub> log<sub>2</sub> <i>w</i><sub><i>i</i></sub></span>)
</th>
<td align="center">0.332
</td>
<td align="center">0.411
</td>
<td align="center">0.521
</td>
<td align="center">0.423
</td>
<td align="center">0.518
</td>
<td align="center"><i>H</i>(<i>A</i>) = 2.205
</td></tr></tbody></table>
<p>For any code that is <i>biunique</i>, meaning that the code is <a href="Variable-length_code#Uniquely_decodable_codes" title="Variable-length code"><i>uniquely decodeable</i></a>, the sum of the probability budgets across all symbols is always less than or equal to one. In this example, the sum is strictly equal to one; as a result, the code is termed a <i>complete</i> code. If this is not the case, one can always derive an equivalent code by adding extra symbols (with associated null probabilities), to make the code complete while keeping it <i>biunique</i>.
</p><p>As defined by <a href="A_Mathematical_Theory_of_Communication" title="A Mathematical Theory of Communication">Shannon (1948)</a>, the information content <i>h</i> (in bits) of each symbol <i>a</i><sub>i</sub> with non-null probability is
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle h(a_{i})=\log _{2}{1 \over w_{i}}.}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>h</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
<mo>=</mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mn>1</mn>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
</mfrac>
</mrow>
<mo>.</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle h(a_{i})=\log _{2}{1 \over w_{i}}.}</annotation>
</semantics>
</math></span><img src="./2799f589d23a9f11345131ef8cef9937c5e7c456.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.171ex; width:16.636ex; height:5.509ex;" alt="{\displaystyle h(a_{i})=\log _{2}{1 \over w_{i}}.}" loading="lazy"></span></dd></dl>
<p>The <a href="Information_entropy" class="mw-redirect" title="Information entropy">entropy</a> <i>H</i> (in bits) is the weighted sum, across all symbols <span class="texhtml"><i>a</i><sub><i>i</i></sub></span> with non-zero probability <span class="texhtml"><i>w</i><sub><i>i</i></sub></span>, of the information content of each symbol:
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-block"><span class="mwe-math-mathml-display mwe-math-mathml-a11y" style="display: none;"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle H(A)=\sum _{w_{i}>0}w_{i}h(a_{i})=\sum _{w_{i}>0}w_{i}\log _{2}{1 \over w_{i}}=-\sum _{w_{i}>0}w_{i}\log _{2}w_{i}.}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>H</mi>
<mo stretchy="false">(</mo>
<mi>A</mi>
<mo stretchy="false">)</mo>
<mo>=</mo>
<munder>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>></mo>
<mn>0</mn>
</mrow>
</munder>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mi>h</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
<mo>=</mo>
<munder>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>></mo>
<mn>0</mn>
</mrow>
</munder>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mn>1</mn>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
</mfrac>
</mrow>
<mo>=</mo>
<mo>−<!-- − --></mo>
<munder>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>></mo>
<mn>0</mn>
</mrow>
</munder>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>.</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle H(A)=\sum _{w_{i}>0}w_{i}h(a_{i})=\sum _{w_{i}>0}w_{i}\log _{2}{1 \over w_{i}}=-\sum _{w_{i}>0}w_{i}\log _{2}w_{i}.}</annotation>
</semantics>
</math></span></span></dd></dl>
<p>(Note: A symbol with zero probability has zero contribution to the entropy, since <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \lim _{w\to 0^{+}}w\log _{2}w=0}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<munder>
<mo movablelimits="true" form="prefix">lim</mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>w</mi>
<mo stretchy="false">→<!-- → --></mo>
<msup>
<mn>0</mn>
<mrow class="MJX-TeXAtom-ORD">
<mo>+</mo>
</mrow>
</msup>
</mrow>
</munder>
<mi>w</mi>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mi>w</mi>
<mo>=</mo>
<mn>0</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \lim _{w\to 0^{+}}w\log _{2}w=0}</annotation>
</semantics>
</math></span><img src="./a25181e71e7e86ccfe31ba981214e6b31f57e7c8.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.338ex; width:17.621ex; height:4.176ex;" alt="{\displaystyle \lim _{w\to 0^{+}}w\log _{2}w=0}" loading="lazy"></span>. So for simplicity, symbols with zero probability can be left out of the formula above.)
</p><p>As a consequence of <a href="Shannon's_source_coding_theorem" title="Shannon's source coding theorem">Shannon's source coding theorem</a>, the entropy is a measure of the smallest codeword length that is theoretically possible for the given alphabet with associated weights. In this example, the weighted average codeword length is 2.25 bits per symbol, only slightly larger than the calculated entropy of 2.205 bits per symbol. So not only is this code optimal in the sense that no other feasible code performs better, but it is very close to the theoretical limit established by Shannon.
</p><p>In general, a Huffman code need not be unique. Thus the set of Huffman codes for a given probability distribution is a non-empty subset of the codes minimizing <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle L(C)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>L</mi>
<mo stretchy="false">(</mo>
<mi>C</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle L(C)}</annotation>
</semantics>
</math></span><img src="./f31c05759c645f617ea0ef5952ea24ccef557716.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:5.158ex; height:2.843ex;" alt="{\displaystyle L(C)}" loading="lazy"></span> for that probability distribution. (However, for each minimizing codeword length assignment, there exists at least one Huffman code with those lengths.)
</p>
<div class="mw-heading mw-heading2"><h2 id="Basic_technique">Basic technique</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Compression">Compression</h3></div>
<p>The technique works by creating a <a href="Binary_tree" title="Binary tree">binary tree</a> of nodes. These can be stored in a regular <a href="Array_data_type" class="mw-redirect" title="Array data type">array</a>, the size of which depends on the number of symbols, <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n}</annotation>
</semantics>
</math></span><img src="./a601995d55609f2d9f5e233e36fbe9ea26011b3b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.395ex; height:1.676ex;" alt="{\displaystyle n}" loading="lazy"></span>. A node can be either a <a href="Leaf_node" class="mw-redirect" title="Leaf node">leaf node</a> or an <a href="Internal_node" class="mw-redirect" title="Internal node">internal node</a>. Initially, all nodes are leaf nodes, which contain the <b>symbol</b> itself, the <b>weight</b> (frequency of appearance) of the symbol and optionally, a link to a <b>parent</b> node which makes it easy to read the code (in reverse) starting from a leaf node. Internal nodes contain a <b>weight</b>, links to <b>two child nodes</b> and an optional link to a <b>parent</b> node. As a common convention, bit '0' represents following the left child and bit '1' represents following the right child. A finished tree has up to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n}</annotation>
</semantics>
</math></span><img src="./a601995d55609f2d9f5e233e36fbe9ea26011b3b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.395ex; height:1.676ex;" alt="{\displaystyle n}" loading="lazy"></span> leaf nodes and <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n-1}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n-1}</annotation>
</semantics>
</math></span><img src="./fbd0b0f32b28f51962943ee9ede4fb34198a2521.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:5.398ex; height:2.343ex;" alt="{\displaystyle n-1}" loading="lazy"></span> internal nodes. A Huffman tree that omits unused symbols produces the most optimal code lengths.
</p><p>The process begins with the leaf nodes containing the probabilities of the symbol they represent. Then, the process takes the two nodes with smallest probability, and creates a new internal node having these two nodes as children. The weight of the new node is set to the sum of the weight of the children. We then apply the process again, on the new internal node and on the remaining nodes (i.e., we exclude the two leaf nodes), we repeat this process until only one node remains, which is the root of the Huffman tree.
</p><p>The simplest construction algorithm uses a <a href="Priority_queue" title="Priority queue">priority queue</a> where the node with lowest probability is given highest priority:
</p>
<ol><li>Create a leaf node for each symbol and add it to the priority queue.</li>
<li>While there is more than one node in the queue:
<ol><li>Remove the two nodes of highest priority (lowest probability) from the queue</li>
<li>Create a new internal node with these two nodes as children and with probability equal to the sum of the two nodes' probabilities.</li>
<li>Add the new node to the queue.</li></ol></li>
<li>The remaining node is the root node and the tree is complete.</li></ol>
<p>Since efficient priority queue data structures require O(log <i>n</i>) time per insertion, and a tree with <i>n</i> leaves has 2<i>n</i>−1 nodes, this algorithm operates in O(<i>n</i> log <i>n</i>) time, where <i>n</i> is the number of symbols.
</p><p>If the symbols are sorted by probability, there is a <a href="Linear-time" class="mw-redirect" title="Linear-time">linear-time</a> (O(<i>n</i>)) method to create a Huffman tree using two <a href="Queue_(data_structure)" class="mw-redirect" title="Queue (data structure)">queues</a>, the first one containing the initial weights (along with pointers to the associated leaves), and combined weights (along with pointers to the trees) being put in the back of the second queue. This assures that the lowest weight is always kept at the front of one of the two queues:
</p>
<ol><li>Start with as many leaves as there are symbols.</li>
<li>Enqueue all leaf nodes into the first queue (by probability in increasing order so that the least likely item is in the head of the queue).</li>
<li>While there is more than one node in the queues:
<ol><li>Dequeue the two nodes with the lowest weight by examining the fronts of both queues.</li>
<li>Create a new internal node, with the two just-removed nodes as children (either node can be either child) and the sum of their weights as the new weight.</li>
<li>Enqueue the new node into the rear of the second queue.</li></ol></li>
<li>The remaining node is the root node; the tree has now been generated.</li></ol>
<p>Once the Huffman tree has been generated, it is traversed to generate a dictionary which maps the symbols to binary codes as follows:
</p>
<ol><li>Start with current node set to the root.</li>
<li>If node is not a leaf node, label the edge to the left child as 0 and the edge to the right child as 1. Repeat the process at both the left child and the right child.</li></ol>
<p>The final encoding of any symbol is then read by a concatenation of the labels on the edges along the path from the root node to the symbol.
</p><p>In many cases, time complexity is not very important in the choice of algorithm here, since <i>n</i> here is the number of symbols in the alphabet, which is typically a very small number (compared to the length of the message to be encoded); whereas complexity analysis concerns the behavior when <i>n</i> grows to be very large.
</p><p>It is generally beneficial to minimize the variance of codeword length. For example, a communication buffer receiving Huffman-encoded data may need to be larger to deal with especially long symbols if the tree is especially unbalanced. To minimize variance, simply break ties between queues by choosing the item in the first queue. This modification will retain the mathematical optimality of the Huffman coding while both minimizing variance and minimizing the length of the longest character code.
</p>
<div class="mw-heading mw-heading3"><h3 id="Decompression">Decompression</h3></div>
<p>Generally speaking, the process of decompression is simply a matter of translating the stream of prefix codes to individual byte values, usually by traversing the Huffman tree node by node as each bit is read from the input stream (reaching a leaf node necessarily terminates the search for that particular byte value). Before this can take place, however, the Huffman tree must be somehow reconstructed. In the simplest case, where character frequencies are fairly predictable, the tree can be preconstructed (and even statistically adjusted on each compression cycle) and thus reused every time, at the expense of at least some measure of compression efficiency. Otherwise, the information to reconstruct the tree must be sent a priori. A naive approach might be to prepend the frequency count of each character to the compression stream. Unfortunately, the overhead in such a case could amount to several kilobytes, so this method has little practical use. If the data is compressed using <a href="Canonical_Huffman_code" title="Canonical Huffman code">canonical encoding</a>, the compression model can be precisely reconstructed with just <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle B\cdot 2^{B}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>B</mi>
<mo>⋅<!-- ⋅ --></mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>B</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle B\cdot 2^{B}}</annotation>
</semantics>
</math></span><img src="./b7c37b838e708201613cb1bcdb5169eb4b163749.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:6.085ex; height:2.676ex;" alt="{\displaystyle B\cdot 2^{B}}" loading="lazy"></span> bits of information (where <span class="texhtml mvar" style="font-style:italic;">B</span> is the number of bits per symbol). Another method is to simply prepend the Huffman tree, bit by bit, to the output stream. For example, assuming that the value of 0 represents a parent node and 1 a leaf node, whenever the latter is encountered the tree building routine simply reads the next 8 bits to determine the character value of that particular leaf. The process continues recursively until the last leaf node is reached; at that point, the Huffman tree will thus be faithfully reconstructed. The overhead using such a method ranges from roughly 2 to 320 bytes (assuming an 8-bit alphabet). Many other techniques are possible as well. In any case, since the compressed data can include unused "trailing bits" the decompressor must be able to determine when to stop producing output. This can be accomplished by either transmitting the length of the decompressed data along with the compression model or by defining a special code symbol to signify the end of input (the latter method can adversely affect code length optimality, however).
</p>
<div class="mw-heading mw-heading2"><h2 id="Main_properties">Main properties</h2></div>
<p>The probabilities used can be generic ones for the application domain that are based on average experience, or they can be the actual frequencies found in the text being compressed.
This requires that a <a href="Frequency_table" class="mw-redirect" title="Frequency table">frequency table</a> must be stored with the compressed text. See the Decompression section above for more information about the various techniques employed for this purpose.
</p>
<div class="mw-heading mw-heading3"><h3 id="Optimality">Optimality</h3></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Arithmetic_coding#Huffman_coding" title="Arithmetic coding">Arithmetic coding § Huffman coding</a></div>
<p>Huffman's original algorithm is optimal for a symbol-by-symbol coding with a known input probability distribution, i.e., separately encoding unrelated symbols in such a data stream. However, it is not optimal when the symbol-by-symbol restriction is dropped, or when the <a href="Probability_mass_function" title="Probability mass function">probability mass functions</a> are unknown. Also, if symbols are not <a href="Independent_and_identically_distributed" class="mw-redirect" title="Independent and identically distributed">independent and identically distributed</a>, a single code may be insufficient for optimality. Other methods such as <a href="Arithmetic_coding" title="Arithmetic coding">arithmetic coding</a> often have better compression capability.
</p><p>Although both aforementioned methods can combine an arbitrary number of symbols for more efficient coding and generally adapt to the actual input statistics, arithmetic coding does so without significantly increasing its computational or algorithmic complexities (though the simplest version is slower and more complex than Huffman coding). Such flexibility is especially useful when input probabilities are not precisely known or vary significantly within the stream. However, Huffman coding is usually faster and arithmetic coding was historically a subject of some concern over <a href="Patent" title="Patent">patent</a> issues. Thus many technologies have historically avoided arithmetic coding in favor of Huffman and other prefix coding techniques. As of mid-2010, the most commonly used techniques for this alternative to Huffman coding have passed into the public domain as the early patents have expired.
</p><p>For a set of symbols with a uniform probability distribution and a number of members which is a <a href="Power_of_two" title="Power of two">power of two</a>, Huffman coding is equivalent to simple binary <a href="Block_code" title="Block code">block encoding</a>, e.g., <a href="ASCII" title="ASCII">ASCII</a> coding. This reflects the fact that compression is not possible with such an input, no matter what the compression method, i.e., doing nothing to the data is the optimal thing to do.
</p><p>Huffman coding is optimal among all methods in any case where each input symbol is a known independent and identically distributed random variable having a probability that is <a href="Dyadic_distribution" title="Dyadic distribution">dyadic</a>. Prefix codes, and thus Huffman coding in particular, tend to have inefficiency on small alphabets, where probabilities often fall between these optimal (dyadic) points. The worst case for Huffman coding can happen when the probability of the most likely symbol far exceeds 2<sup>−1</sup> = 0.5, making the upper limit of inefficiency unbounded.
</p><p>There are two related approaches for getting around this particular inefficiency while still using Huffman coding. Combining a fixed number of symbols together ("blocking") often increases (and never decreases) compression. As the size of the block approaches infinity, Huffman coding theoretically approaches the entropy limit, i.e., optimal compression.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> However, blocking arbitrarily large groups of symbols is impractical, as the complexity of a Huffman code is linear in the number of possibilities to be encoded, a number that is exponential in the size of a block. This limits the amount of blocking that is done in practice.
</p><p>A practical alternative, in widespread use, is <a href="Run-length_encoding" title="Run-length encoding">run-length encoding</a>. This technique adds one step in advance of entropy coding, specifically counting (runs) of repeated symbols, which are then encoded. For the simple case of <a href="Bernoulli_process" title="Bernoulli process">Bernoulli processes</a>, <a href="Golomb_coding" title="Golomb coding">Golomb coding</a> is optimal among prefix codes for coding run length, a fact proved via the techniques of Huffman coding.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> A similar approach is taken by fax machines using <a href="Modified_Huffman_coding" title="Modified Huffman coding">modified Huffman coding</a>. However, run-length coding is not as adaptable to as many input types as other compression technologies.
</p>
<div class="mw-heading mw-heading2"><h2 id="Variations">Variations</h2></div>
<p>Many variations of Huffman coding exist,<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> some of which use a Huffman-like algorithm, and others of which find optimal prefix codes (while, for example, putting different restrictions on the output). Note that, in the latter case, the method need not be Huffman-like, and, indeed, need not even be <a href="Polynomial_time" class="mw-redirect" title="Polynomial time">polynomial time</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="n-ary_Huffman_coding"><i>n</i>-ary Huffman coding</h3></div>
<p>The <b><i>n</i>-ary Huffman</b> algorithm uses an alphabet of size <i>n</i>, typically {0, 1, ..., n-1}, to encode messages and build an <i>n</i>-ary tree. This approach was considered by Huffman in his original paper. The same algorithm applies as for binary (<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n=2}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
<mo>=</mo>
<mn>2</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n=2}</annotation>
</semantics>
</math></span><img src="./a02c8bd752d2cc859747ca1f3a508281bdbc3b34.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:5.656ex; height:2.176ex;" alt="{\displaystyle n=2}" loading="lazy"></span>) codes, but instead of combining the two least likely symbols, the <i>n</i> least likely symbols are grouped together.
</p><p>Note that for <i>n</i> > 2, not all sets of source words can properly form a complete <i>n</i>-ary tree for Huffman coding. In these cases, additional placeholder symbols with 0 probability may need to be added. This is because the structure of the tree needs to repeatedly join <i>n</i> branches into one - also known as an "<i>n</i> to 1" combination. For binary coding, this is a "2 to 1" combination, which works with any number of symbols. For <i>n</i>-ary coding, a complete tree is only possible when the total number of symbols (real + placeholders) leaves a remainder of 1 when divided by (n-1). <sup id="cite_ref-:0_1-1" class="reference"><a href="#cite_note-:0-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Adaptive_Huffman_coding">Adaptive Huffman coding</h3></div>
<p>A variation called <b><a href="Adaptive_Huffman_coding" title="Adaptive Huffman coding">adaptive Huffman coding</a></b> involves calculating the probabilities dynamically based on recent actual frequencies in the sequence of source symbols, and changing the coding tree structure to match the updated probability estimates. It is used rarely in practice, since the cost of updating the tree makes it slower than optimized <a href="Arithmetic_coding#Adaptive_arithmetic_coding" title="Arithmetic coding">adaptive arithmetic coding</a>, which is more flexible and has better compression.
</p>
<div class="mw-heading mw-heading3"><h3 id="Huffman_template_algorithm">Huffman template algorithm</h3></div>
<p>Most often, the weights used in implementations of Huffman coding represent numeric probabilities, but the algorithm given above does not require this; it requires only that the weights form a <a href="Total_order" title="Total order">totally ordered</a> <a href="Monoid#Commutative_monoid" title="Monoid">commutative monoid</a>, meaning a way to order weights and to add them. The <b>Huffman template algorithm</b> enables one to use any kind of weights (costs, frequencies, pairs of weights, non-numerical weights) and one of many combining methods (not just addition). Such algorithms can solve other minimization problems, such as minimizing <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \max _{i}\left[w_{i}+\mathrm {length} \left(c_{i}\right)\right]}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<munder>
<mo movablelimits="true" form="prefix">max</mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</munder>
<mrow>
<mo>[</mo>
<mrow>
<msub>
<mi>w</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>+</mo>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="normal">l</mi>
<mi mathvariant="normal">e</mi>
<mi mathvariant="normal">n</mi>
<mi mathvariant="normal">g</mi>
<mi mathvariant="normal">t</mi>
<mi mathvariant="normal">h</mi>
</mrow>
<mrow>
<mo>(</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<mo>)</mo>
</mrow>
</mrow>
<mo>]</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \max _{i}\left[w_{i}+\mathrm {length} \left(c_{i}\right)\right]}</annotation>
</semantics>
</math></span><img src="./1a80c0b460cf854976824c251037db43d7ed1810.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.005ex; width:21.645ex; height:4.009ex;" alt="{\displaystyle \max _{i}\left[w_{i}+\mathrm {length} \left(c_{i}\right)\right]}" loading="lazy"></span>, a problem first applied to circuit design.
</p>
<div class="mw-heading mw-heading3"><h3 id="Length-limited_Huffman_coding/minimum_variance_Huffman_coding">Length-limited Huffman coding/minimum variance Huffman coding</h3></div>
<p><b>Length-limited Huffman coding</b> is a variant where the goal is still to achieve a minimum weighted path length, but there is an additional restriction that the length of each codeword must be less than a given constant. The <a href="Package-merge_algorithm" title="Package-merge algorithm">package-merge algorithm</a> solves this problem with a simple <a href="Greedy_algorithm" title="Greedy algorithm">greedy</a> approach very similar to that used by Huffman's algorithm. Its time complexity is <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(nL)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mi>L</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(nL)}</annotation>
</semantics>
</math></span><img src="./a115e943ca1827d51630227813a09e82b0980b8c.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:6.56ex; height:2.843ex;" alt="{\displaystyle O(nL)}" loading="lazy"></span>, where <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle L}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>L</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle L}</annotation>
</semantics>
</math></span><img src="./103168b86f781fe6e9a4a87b8ea1cebe0ad4ede8.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.583ex; height:2.176ex;" alt="{\displaystyle L}" loading="lazy"></span> is the maximum length of a codeword. No algorithm is known to solve this problem in <a href="Big_O_notation#Orders_of_common_functions" title="Big O notation"><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(n)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(n)}</annotation>
</semantics>
</math></span><img src="./34109fe397fdcff370079185bfdb65826cb5565a.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:4.977ex; height:2.843ex;" alt="{\displaystyle O(n)}" loading="lazy"></span> or <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(n\log n)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mi>log</mi>
<mo><!-- --></mo>
<mi>n</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(n\log n)}</annotation>
</semantics>
</math></span><img src="./9d2320768fb54880ca4356e61f60eb02a3f9d9f1.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:10.118ex; height:2.843ex;" alt="{\displaystyle O(n\log n)}" loading="lazy"></span></a> time, unlike the presorted and unsorted conventional Huffman problems, respectively.
</p>
<div class="mw-heading mw-heading3"><h3 id="Huffman_coding_with_unequal_letter_costs">Huffman coding with unequal letter costs</h3></div>
<p>In the standard Huffman coding problem, it is assumed that each symbol in the set that the code words are constructed from has an equal cost to transmit: a code word whose length is <i>N</i> digits will always have a cost of <i>N</i>, no matter how many of those digits are 0s, how many are 1s, etc. When working under this assumption, minimizing the total cost of the message and minimizing the total number of digits are the same thing.
</p><p><i>Huffman coding with unequal letter costs</i> is the generalization without this assumption: the letters of the encoding alphabet may have non-uniform lengths, due to characteristics of the transmission medium. An example is the encoding alphabet of <a href="Morse_code" title="Morse code">Morse code</a>, where a 'dash' takes longer to send than a 'dot', and therefore the cost of a dash in transmission time is higher. The goal is still to minimize the weighted average codeword length, but it is no longer sufficient just to minimize the number of symbols used by the message. No algorithm is known to solve this in the same manner or with the same efficiency as conventional Huffman coding, though it has been solved by <a href="Richard_M._Karp" title="Richard M. Karp">Richard M. Karp</a><sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> whose solution has been refined for the case of integer costs by Mordecai J. Golin.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Optimal_alphabetic_binary_trees_(Hu–Tucker_coding)">Optimal alphabetic binary trees (Hu–Tucker coding)</h3></div>
<p>In the standard Huffman coding problem, it is assumed that any codeword can correspond to any input symbol. In the alphabetic version, the alphabetic order of inputs and outputs must be identical. Thus, for example, <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle A=\left\{a,b,c\right\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>A</mi>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow>
<mi>a</mi>
<mo>,</mo>
<mi>b</mi>
<mo>,</mo>
<mi>c</mi>
</mrow>
<mo>}</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle A=\left\{a,b,c\right\}}</annotation>
</semantics>
</math></span><img src="./5253725abd6a5bf17a59383638d748fc80e2eec0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:12.469ex; height:2.843ex;" alt="{\displaystyle A=\left\{a,b,c\right\}}" loading="lazy"></span> could not be assigned code <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle H\left(A,C\right)=\left\{00,1,01\right\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>H</mi>
<mrow>
<mo>(</mo>
<mrow>
<mi>A</mi>
<mo>,</mo>
<mi>C</mi>
</mrow>
<mo>)</mo>
</mrow>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow>
<mn>00</mn>
<mo>,</mo>
<mn>1</mn>
<mo>,</mo>
<mn>01</mn>
</mrow>
<mo>}</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle H\left(A,C\right)=\left\{00,1,01\right\}}</annotation>
</semantics>
</math></span><img src="./c2940f22ba0b9fd277c1387648c319cae29a836b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:22.107ex; height:2.843ex;" alt="{\displaystyle H\left(A,C\right)=\left\{00,1,01\right\}}" loading="lazy"></span>, but instead should be assigned either <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle H\left(A,C\right)=\left\{00,01,1\right\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>H</mi>
<mrow>
<mo>(</mo>
<mrow>
<mi>A</mi>
<mo>,</mo>
<mi>C</mi>
</mrow>
<mo>)</mo>
</mrow>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow>
<mn>00</mn>
<mo>,</mo>
<mn>01</mn>
<mo>,</mo>
<mn>1</mn>
</mrow>
<mo>}</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle H\left(A,C\right)=\left\{00,01,1\right\}}</annotation>
</semantics>
</math></span><img src="./a2f3f93c449df17971e9f5e0587e6168514b7822.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:22.107ex; height:2.843ex;" alt="{\displaystyle H\left(A,C\right)=\left\{00,01,1\right\}}" loading="lazy"></span> or <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle H\left(A,C\right)=\left\{0,10,11\right\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>H</mi>
<mrow>
<mo>(</mo>
<mrow>
<mi>A</mi>
<mo>,</mo>
<mi>C</mi>
</mrow>
<mo>)</mo>
</mrow>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow>
<mn>0</mn>
<mo>,</mo>
<mn>10</mn>
<mo>,</mo>
<mn>11</mn>
</mrow>
<mo>}</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle H\left(A,C\right)=\left\{0,10,11\right\}}</annotation>
</semantics>
</math></span><img src="./19021e4b2119067cacc69927449535e2dae0ee8d.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:22.107ex; height:2.843ex;" alt="{\displaystyle H\left(A,C\right)=\left\{0,10,11\right\}}" loading="lazy"></span>. This is also known as the <b>Hu–Tucker</b> problem, after <a href="T._C._Hu" title="T. C. Hu">T. C. Hu</a> and <a href="Alan_Tucker" title="Alan Tucker">Alan Tucker</a>, the authors of the paper presenting the first <a href="Time_complexity#Quasilinear_time" title="Time complexity"><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle O(n\log n)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>O</mi>
<mo stretchy="false">(</mo>
<mi>n</mi>
<mi>log</mi>
<mo><!-- --></mo>
<mi>n</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle O(n\log n)}</annotation>
</semantics>
</math></span><img src="./9d2320768fb54880ca4356e61f60eb02a3f9d9f1.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:10.118ex; height:2.843ex;" alt="{\displaystyle O(n\log n)}" loading="lazy"></span>-time</a> solution to this optimal binary alphabetic problem,<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> which has some similarities to Huffman algorithm, but is not a variation of this algorithm. A later method, the <a href="Garsia%E2%80%93Wachs_algorithm" title="Garsia–Wachs algorithm">Garsia–Wachs algorithm</a> of <a href="Adriano_Garsia" title="Adriano Garsia">Adriano Garsia</a> and <a href="Michelle_L._Wachs" title="Michelle L. Wachs">Michelle L. Wachs</a> (1977), uses simpler logic to perform the same comparisons in the same total time bound. These optimal alphabetic binary trees are often used as <a href="Binary_search_tree" title="Binary search tree">binary search trees</a>.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="The_canonical_Huffman_code">The canonical Huffman code</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Canonical_Huffman_code" title="Canonical Huffman code">Canonical Huffman code</a></div>
<p>If weights corresponding to the alphabetically ordered inputs are in numerical order, the Huffman code has the same lengths as the optimal alphabetic code, which can be found from calculating these lengths, rendering Hu–Tucker coding unnecessary. The code resulting from numerically (re-)ordered input is sometimes called the <i>canonical Huffman code</i> and is often the code used in practice, due to ease of encoding/decoding. The technique for finding this code is sometimes called <b>Huffman–Shannon–Fano coding</b>, since it is optimal like Huffman coding, but alphabetic in weight probability, like <a href="Shannon%E2%80%93Fano_coding" title="Shannon–Fano coding">Shannon–Fano coding</a>. The Huffman–Shannon–Fano code corresponding to the example is <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \{000,001,01,10,11\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo fence="false" stretchy="false">{</mo>
<mn>000</mn>
<mo>,</mo>
<mn>001</mn>
<mo>,</mo>
<mn>01</mn>
<mo>,</mo>
<mn>10</mn>
<mo>,</mo>
<mn>11</mn>
<mo fence="false" stretchy="false">}</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \{000,001,01,10,11\}}</annotation>
</semantics>
</math></span><img src="./66716f2fabf56efc6c70dcd44dfb4c0b5937ed8b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:20.41ex; height:2.843ex;" alt="{\displaystyle \{000,001,01,10,11\}}" loading="lazy"></span>, which, having the same codeword lengths as the original solution, is also optimal. But in <i>canonical Huffman code</i>, the result is <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \{110,111,00,01,10\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo fence="false" stretchy="false">{</mo>
<mn>110</mn>
<mo>,</mo>
<mn>111</mn>
<mo>,</mo>
<mn>00</mn>
<mo>,</mo>
<mn>01</mn>
<mo>,</mo>
<mn>10</mn>
<mo fence="false" stretchy="false">}</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \{110,111,00,01,10\}}</annotation>
</semantics>
</math></span><img src="./7fed0dac074f7e39fcfe91e5cae47c22bbb2f4ac.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:20.41ex; height:2.843ex;" alt="{\displaystyle \{110,111,00,01,10\}}" loading="lazy"></span>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Applications">Applications</h2></div>
<p><a href="Arithmetic_coding" title="Arithmetic coding">Arithmetic coding</a> and Huffman coding produce equivalent results — achieving entropy — when every symbol has a probability of the form 1/2<sup><i>k</i></sup>. In other circumstances, arithmetic coding can offer better compression than Huffman coding because — intuitively — its "code words" can have effectively non-integer bit lengths, whereas code words in prefix codes such as Huffman codes can only have an integer number of bits. Therefore, a code word of length <i>k</i> only optimally matches a symbol of probability 1/2<sup><i>k</i></sup> and other probabilities are not represented optimally; whereas the code word length in arithmetic coding can be made to exactly match the true probability of the symbol. This difference is especially striking for small alphabet sizes.
</p><p>Prefix codes nevertheless remain in wide use because of their simplicity, high speed, and <a href="Arithmetic_coding#History_and_patents" title="Arithmetic coding">lack of patent coverage</a>. They are often used as a "back-end" to other compression methods. <a href="Deflate" title="Deflate">Deflate</a> (<a href="PKZIP" title="PKZIP">PKZIP</a>'s algorithm) and multimedia <a href="Codec" title="Codec">codecs</a> such as <a href="JPEG" title="JPEG">JPEG</a> and <a href="MP3" title="MP3">MP3</a> have a front-end model and <a href="Quantization_(signal_processing)" title="Quantization (signal processing)">quantization</a> followed by the use of prefix codes; these are often called "Huffman codes" even though most applications use pre-defined variable-length codes rather than codes designed using Huffman's algorithm.
</p>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1290876196">
/* start https://en.wikipedia.org/ */
.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .infobox .side-box{font-size:100%}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1237033735">
/* start https://en.wikipedia.org/ */
@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}
/* end https://en.wikipedia.org/ */
</style><div class="side-box side-box-right sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */
.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}
/* end https://en.wikipedia.org/ */
</style>
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">Wikimedia Commons has media related to <span style="font-weight: bold; font-style: italic;"><a href="https://commons.wikimedia.org/wiki/Category:Huffman_coding" class="extiw external" title="commons:Category:Huffman coding">Huffman coding</a></span>.</div></div>
</div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-:0-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-:0_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-:0_1-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFHuffman1952" class="citation journal cs1"><a href="David_A._Huffman" title="David A. Huffman">Huffman, D.</a> (1952). <a rel="nofollow" class="external text" href="http://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf">"A Method for the Construction of Minimum-Redundancy Codes"</a> <span class="cs1-format">(PDF)</span>. <i><a href="Proceedings_of_the_IRE" class="mw-redirect" title="Proceedings of the IRE">Proceedings of the IRE</a></i>. <b>40</b> (9): <span class="nowrap">1098–</span>1101. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FJRPROC.1952.273898">10.1109/JRPROC.1952.273898</a>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFVan_Leeuwen1976" class="citation journal cs1"><a href="Jan_van_Leeuwen" title="Jan van Leeuwen">Van Leeuwen, Jan</a> (1976). <a rel="nofollow" class="external text" href="http://www.staff.science.uu.nl/~leeuw112/huffman.pdf">"On the construction of Huffman trees"</a> <span class="cs1-format">(PDF)</span>. <i>ICALP</i>: <span class="nowrap">382–</span>410<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-02-20</span></span>.</cite></span>
</li>
<li id="cite_note-LiDrew2014-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-LiDrew2014_3-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFZe-Nian_LiMark_S._DrewJiangchuan_Liu2014" class="citation book cs1">Ze-Nian Li; Mark S. Drew; Jiangchuan Liu (2014-04-09). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=R6vBBAAAQBAJ"><i>Fundamentals of Multimedia</i></a>. Springer Science & Business Media. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-3-319-05290-8</bdi>.</cite></span>
</li>
<li id="cite_note-PCS2015-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-PCS2015_4-0">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://ieeexplore.ieee.org/document/7170048">J. Duda, K. Tahboub, N. J. Gadil, E. J. Delp, <i>The use of asymmetric numeral systems as an accurate replacement for Huffman coding</i></a>, Picture Coding Symposium, 2015.</span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite id="CITEREFHuffman1991" class="citation journal cs1">Huffman, Ken (1991). <a rel="nofollow" class="external text" href="http://www.huffmancoding.com/my-uncle/scientific-american">"Profile: David A. Huffman: Encoding the "Neatness" of Ones and Zeroes"</a>. <i><a href="Scientific_American" title="Scientific American">Scientific American</a></i>: <span class="nowrap">54–</span>58.</cite></span>
</li>
<li id="cite_note-kleinberg&Tardos-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-kleinberg&Tardos_6-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFKleinbergTardos2005" class="citation book cs1">Kleinberg, Jon; Tardos, Eva (2005-03-16). <a rel="nofollow" class="external text" href="https://www.pearson.com/en-us/subject-catalog/p/algorithm-design/P200000003259/9780137546350"><i>Algorithm Design</i></a> (1 ed.). <a href="Pearson_Education" title="Pearson Education">Pearson Education</a>. p. 165. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>9780321295354</bdi><span class="reference-accessdate">. Retrieved <span class="nowrap">2025-01-26</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite id="CITEREFGribov2017" class="citation arxiv cs1">Gribov, Alexander (2017-04-10). "Optimal Compression of a Polyline with Segments and Arcs". <a href="ArXiv_(identifier)" class="mw-redirect" title="ArXiv (identifier)">arXiv</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://arxiv.org/abs/1604.07476">1604.07476</a></span> [<a rel="nofollow" class="external text" href="https://arxiv.org/archive/cs.CG">cs.CG</a>].</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite id="CITEREFGallagervan_Voorhis1975" class="citation journal cs1">Gallager, R.G.; van Voorhis, D.C. (1975). "Optimal source codes for geometrically distributed integer alphabets". <i><a href="IEEE_Transactions_on_Information_Theory" title="IEEE Transactions on Information Theory">IEEE Transactions on Information Theory</a></i>. <b>21</b> (2): <span class="nowrap">228–</span>230. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FTIT.1975.1055357">10.1109/TIT.1975.1055357</a>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite id="CITEREFAbrahams1997" class="citation book cs1 cs1-prop-location-test">Abrahams, J. (1997-06-11). "Code and parse trees for lossless source encoding". Written at Arlington, VA, USA. <i>Proceedings. Compression and Complexity of SEQUENCES 1997 (Cat. No.97TB100171)</i>. Division of Mathematics, Computer & Information Sciences, <a href="Office_of_Naval_Research" title="Office of Naval Research">Office of Naval Research</a> (ONR). Salerno: <a href="IEEE" class="mw-redirect" title="IEEE">IEEE</a>. pp. <span class="nowrap">145–</span>171. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.589.4726">10.1.1.589.4726</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FSEQUEN.1997.666911">10.1109/SEQUEN.1997.666911</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-8186-8132-2</bdi>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:124587565">124587565</a>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite id="CITEREFKarp1961" class="citation journal cs1"><a href="Richard_M._Karp" title="Richard M. Karp">Karp, Richard M.</a> (1961-01-31). <span class="id-lock-subscription" title="Paid subscription required"><a rel="nofollow" class="external text" href="https://ieeexplore.ieee.org/document/1057615">"Minimum-redundancy coding for the discrete noiseless channel"</a></span>. <i>IRE Transactions on Information Theory</i>. <b>7</b> (1). IEEE: <span class="nowrap">27–</span>38. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FTIT.1961.1057615">10.1109/TIT.1961.1057615</a>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite id="CITEREFGolin1998" class="citation journal cs1">Golin, Mordekai J. (January 1998). <a rel="nofollow" class="external text" href="https://page.mi.fu-berlin.de/rote/Papers/pdf/A+dynamic+programming+algorithm+for+constructing+optimal+prefix-free+codes+for+unequal+letter+costs.pdf">"A Dynamic Programming Algorithm for Constructing Optimal Prefix-Free Codes with Unequal Letter Costs"</a> <span class="cs1-format">(PDF)</span>. <i>IEEE Transactions on Information Theory</i>. <b>44</b> (5) (published 1998-09-01): <span class="nowrap">1770–</span>1781. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2F18.705558">10.1109/18.705558</a>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:2265146">2265146</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-09-10</span></span>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite id="CITEREFHuTucker1971" class="citation journal cs1"><a href="T._C._Hu" title="T. C. Hu">Hu, T. C.</a>; <a href="Alan_Tucker" title="Alan Tucker">Tucker, A. C.</a> (1971). "Optimal Computer Search Trees and Variable-Length Alphabetical Codes". <i>SIAM Journal on Applied Mathematics</i>. <b>21</b> (4): 514. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1137%2F0121057">10.1137/0121057</a>. <a href="JSTOR_(identifier)" class="mw-redirect" title="JSTOR (identifier)">JSTOR</a> <a rel="nofollow" class="external text" href="https://www.jstor.org/stable/2099603">2099603</a>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite id="CITEREFKnuth1998" class="citation cs2"><a href="Donald_Knuth" title="Donald Knuth">Knuth, Donald E.</a> (1998), "Algorithm G (Garsia–Wachs algorithm for optimum binary trees)", <i>The Art of Computer Programming, Vol. 3: Sorting and Searching</i> (2nd ed.), Addison–Wesley, pp. <span class="nowrap">451–</span>453</cite>. See also History and bibliography, pp. 453–454.</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="Bibliography">Bibliography</h2></div>
<ul><li><a href="Thomas_H._Cormen" title="Thomas H. Cormen">Thomas H. Cormen</a>, <a href="Charles_E._Leiserson" title="Charles E. Leiserson">Charles E. Leiserson</a>, <a href="Ronald_L._Rivest" class="mw-redirect" title="Ronald L. Rivest">Ronald L. Rivest</a>, and <a href="Clifford_Stein" title="Clifford Stein">Clifford Stein</a>. <i><a href="Introduction_to_Algorithms" title="Introduction to Algorithms">Introduction to Algorithms</a></i>, Second Edition. MIT Press and McGraw-Hill, 2001. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-262-03293-7</bdi>. Section 16.3, pp. 385–392.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://rosettacode.org/wiki/Huffman_coding">Huffman coding in various languages on Rosetta Code</a></li>
<li><a rel="nofollow" class="external text" href="https://gist.github.com/jasonrdsouza/1c9c895f43497d15eb2e">Huffman codes (python implementation)</a></li>
<li><a rel="nofollow" class="external text" href="https://github.com/gdavidbutler/canonicalHuffman">Canonical Huffman codes (C implementation)</a></li>
<li><a rel="nofollow" class="external text" href="https://www.tinyray.com/huffman">A visualization of Huffman coding</a></li></ul>
<div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1236075235">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style></div><div role="navigation" class="navbox" aria-labelledby="Data_compression_methods241" style="padding:3px"><table class="nowraplinks hlist mw-collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div id="Data_compression_methods241" style="font-size:114%;margin:0 4em"><a href="Data_compression" title="Data compression">Data compression</a> methods</div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Lossless_compression" title="Lossless compression">Lossless</a><br>type</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"></div><table class="nowraplinks navbox-subgroup" style="border-spacing:0"><tbody><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Entropy_coding" title="Entropy coding">Entropy</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Adaptive_coding" title="Adaptive coding">Adaptive coding</a></li>
<li><a href="Arithmetic_coding" title="Arithmetic coding">Arithmetic</a></li>
<li><a href="Asymmetric_numeral_systems" title="Asymmetric numeral systems">Asymmetric numeral systems</a></li>
<li><a href="Golomb_coding" title="Golomb coding">Golomb</a></li>
<li>
<ul><li><a href="Adaptive_Huffman_coding" title="Adaptive Huffman coding">Adaptive</a></li>
<li><a href="Canonical_Huffman_code" title="Canonical Huffman code">Canonical</a></li>
<li><a href="Modified_Huffman_coding" title="Modified Huffman coding">Modified</a></li></ul></li>
<li><a href="Range_coding" title="Range coding">Range</a></li>
<li><a href="Shannon_coding" title="Shannon coding">Shannon</a></li>
<li><a href="Shannon%E2%80%93Fano_coding" title="Shannon–Fano coding">Shannon–Fano</a></li>
<li><a href="Shannon%E2%80%93Fano%E2%80%93Elias_coding" title="Shannon–Fano–Elias coding">Shannon–Fano–Elias</a></li>
<li><a href="Tunstall_coding" title="Tunstall coding">Tunstall</a></li>
<li><a href="Unary_coding" title="Unary coding">Unary</a></li>
<li><a href="Universal_code_(data_compression)" title="Universal code (data compression)">Universal</a>
<ul><li><a href="Exponential-Golomb_coding" title="Exponential-Golomb coding">Exp-Golomb</a></li>
<li><a href="Fibonacci_coding" title="Fibonacci coding">Fibonacci</a></li>
<li><a href="Elias_gamma_coding" title="Elias gamma coding">Gamma</a></li>
<li><a href="Levenshtein_coding" title="Levenshtein coding">Levenshtein</a></li></ul></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Dictionary_coder" title="Dictionary coder">Dictionary</a></th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Byte-pair_encoding" title="Byte-pair encoding">Byte-pair encoding</a></li>
<li><a href="LZ77_and_LZ78" title="LZ77 and LZ78">Lempel–Ziv</a>
<ul><li><a href="842_(compression_algorithm)" title="842 (compression algorithm)">842</a></li>
<li><a href="LZ4_(compression_algorithm)" title="LZ4 (compression algorithm)">LZ4</a></li>
<li><a href="LZJB" class="mw-redirect" title="LZJB">LZJB</a></li>
<li><a href="Lempel%E2%80%93Ziv%E2%80%93Oberhumer" title="Lempel–Ziv–Oberhumer">LZO</a></li>
<li><a href="LZRW" title="LZRW">LZRW</a></li>
<li><a href="Lempel%E2%80%93Ziv%E2%80%93Storer%E2%80%93Szymanski" title="Lempel–Ziv–Storer–Szymanski">LZSS</a></li>
<li><a href="Lempel%E2%80%93Ziv%E2%80%93Welch" title="Lempel–Ziv–Welch">LZW</a></li>
<li><a href="LZWL" title="LZWL">LZWL</a></li>
<li><a href="Snappy_(compression)" title="Snappy (compression)">Snappy</a></li></ul></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Other</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Burrows%E2%80%93Wheeler_transform" title="Burrows–Wheeler transform">BWT</a></li>
<li><a href="Context_tree_weighting" title="Context tree weighting">CTW</a></li>
<li><a href="Context_mixing" title="Context mixing">CM</a></li>
<li><a href="Delta_encoding" title="Delta encoding">Delta</a>
<ul><li><a href="Incremental_encoding" title="Incremental encoding">Incremental</a></li></ul></li>
<li><a href="Dynamic_Markov_compression" title="Dynamic Markov compression">DMC</a></li>
<li><a href="Differential_pulse-code_modulation" title="Differential pulse-code modulation">DPCM</a></li>
<li><a href="Grammar-based_code" title="Grammar-based code">Grammar</a>
<ul><li><a href="Re-Pair" title="Re-Pair">Re-Pair</a></li>
<li><a href="Sequitur_algorithm" title="Sequitur algorithm">Sequitur</a></li></ul></li>
<li><a href="Discrete_cosine_transform" title="Discrete cosine transform">LDCT</a></li>
<li><a href="Move-to-front_transform" title="Move-to-front transform">MTF</a></li>
<li><a href="PAQ" title="PAQ">PAQ</a></li>
<li><a href="Prediction_by_partial_matching" title="Prediction by partial matching">PPM</a></li>
<li><a href="Run-length_encoding" title="Run-length encoding">RLE</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Hybrid</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li>LZ77 + Huffman
<ul><li><a href="Deflate" title="Deflate">Deflate</a></li>
<li><a href="LZX" title="LZX">LZX</a></li>
<li><a href="Lempel%E2%80%93Ziv%E2%80%93Stac" title="Lempel–Ziv–Stac">LZS</a></li></ul></li>
<li>LZ77 + ANS
<ul><li><a href="LZFSE" title="LZFSE">LZFSE</a></li></ul></li>
<li>LZ77 + Huffman + ANS
<ul><li><a href="Zstd" title="Zstd">Zstandard</a></li></ul></li>
<li>LZ77 + Huffman + context
<ul><li><a href="Brotli" title="Brotli">Brotli</a></li></ul></li>
<li>LZSS + Huffman
<ul><li><a href="LHA_(file_format)" title="LHA (file format)">LHA/LZH</a></li></ul></li>
<li>LZ77 + Range
<ul><li><a href="LZMA" title="LZMA">LZMA</a></li>
<li>LZHAM</li></ul></li>
<li>RLE + BWT + MTF + Huffman
<ul><li><a href="Bzip2" title="Bzip2">bzip2</a></li></ul></li></ul>
</div></td></tr></tbody></table><div></div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Lossy_compression" title="Lossy compression">Lossy</a><br>type</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"></div><table class="nowraplinks navbox-subgroup" style="border-spacing:0"><tbody><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Transform_coding" title="Transform coding">Transform</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Discrete_cosine_transform" title="Discrete cosine transform">Discrete cosine transform</a>
<ul><li><a href="Discrete_cosine_transform" title="Discrete cosine transform">DCT</a></li>
<li><a href="Modified_discrete_cosine_transform" title="Modified discrete cosine transform">MDCT</a></li></ul></li>
<li><a href="Discrete_sine_transform" title="Discrete sine transform">DST</a></li>
<li><a href="Fast_Fourier_transform" title="Fast Fourier transform">FFT</a></li>
<li><a href="Wavelet_transform" title="Wavelet transform">Wavelet</a>
<ul><li><a href="Daubechies_wavelet" title="Daubechies wavelet">Daubechies</a></li>
<li><a href="Discrete_wavelet_transform" title="Discrete wavelet transform">DWT</a></li>
<li><a href="Set_partitioning_in_hierarchical_trees" title="Set partitioning in hierarchical trees">SPIHT</a></li></ul></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Predictive</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Differential_pulse-code_modulation" title="Differential pulse-code modulation">DPCM</a>
<ul><li><a href="Adaptive_differential_pulse-code_modulation" title="Adaptive differential pulse-code modulation">ADPCM</a></li></ul></li>
<li><a href="Linear_predictive_coding" title="Linear predictive coding">LPC</a>
<ul><li><a href="Algebraic_code-excited_linear_prediction" title="Algebraic code-excited linear prediction">ACELP</a></li>
<li><a href="Code-excited_linear_prediction" title="Code-excited linear prediction">CELP</a></li>
<li><a href="Log_area_ratio" title="Log area ratio">LAR</a></li>
<li><a href="Line_spectral_pairs" title="Line spectral pairs">LSP</a></li>
<li><a href="Warped_linear_predictive_coding" title="Warped linear predictive coding">WLPC</a></li></ul></li>
<li>Motion
<ul><li><a href="Motion_compensation" title="Motion compensation">Compensation</a></li>
<li><a href="Motion_estimation" title="Motion estimation">Estimation</a></li>
<li><a href="Motion_vector" class="mw-redirect" title="Motion vector">Vector</a></li></ul></li>
<li><a href="Psychoacoustics" title="Psychoacoustics">Psychoacoustic</a></li></ul>
</div></td></tr></tbody></table><div></div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Data_compression#Audio" title="Data compression">Audio</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"></div><table class="nowraplinks navbox-subgroup" style="border-spacing:0"><tbody><tr><th scope="row" class="navbox-group" style="width:1%">Concepts</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Bit_rate" title="Bit rate">Bit rate</a>
<ul><li><a href="Average_bitrate" title="Average bitrate">ABR</a></li>
<li><a href="Constant_bitrate" title="Constant bitrate">CBR</a></li>
<li><a href="Variable_bitrate" title="Variable bitrate">VBR</a></li></ul></li>
<li><a href="Companding" title="Companding">Companding</a></li>
<li><a href="Convolution" title="Convolution">Convolution</a></li>
<li><a href="Dynamic_range" title="Dynamic range">Dynamic range</a></li>
<li><a href="Latency_(audio)" title="Latency (audio)">Latency</a></li>
<li><a href="Nyquist%E2%80%93Shannon_sampling_theorem" title="Nyquist–Shannon sampling theorem">Nyquist–Shannon theorem</a></li>
<li><a href="Sampling_(signal_processing)" title="Sampling (signal processing)">Sampling</a></li>
<li><a href="Silence_compression" title="Silence compression">Silence compression</a></li>
<li><a href="Sound_quality" title="Sound quality">Sound quality</a></li>
<li><a href="Speech_coding" title="Speech coding">Speech coding</a></li>
<li><a href="Sub-band_coding" title="Sub-band coding">Sub-band coding</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Audio_codec" title="Audio codec">Codec</a><br>parts</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="A-law_algorithm" title="A-law algorithm">A-law</a></li>
<li><a href="%CE%9C-law_algorithm" title="Μ-law algorithm">μ-law</a></li>
<li><a href="Differential_pulse-code_modulation" title="Differential pulse-code modulation">DPCM</a>
<ul><li><a href="Adaptive_differential_pulse-code_modulation" title="Adaptive differential pulse-code modulation">ADPCM</a></li>
<li><a href="Delta_modulation" title="Delta modulation">DM</a></li></ul></li>
<li><a href="Fourier_transform" title="Fourier transform">FT</a>
<ul><li><a href="Fast_Fourier_transform" title="Fast Fourier transform">FFT</a></li></ul></li>
<li><a href="Linear_predictive_coding" title="Linear predictive coding">LPC</a>
<ul><li><a href="Algebraic_code-excited_linear_prediction" title="Algebraic code-excited linear prediction">ACELP</a></li>
<li><a href="Code-excited_linear_prediction" title="Code-excited linear prediction">CELP</a></li>
<li><a href="Log_area_ratio" title="Log area ratio">LAR</a></li>
<li><a href="Line_spectral_pairs" title="Line spectral pairs">LSP</a></li>
<li><a href="Warped_linear_predictive_coding" title="Warped linear predictive coding">WLPC</a></li></ul></li>
<li><a href="Modified_discrete_cosine_transform" title="Modified discrete cosine transform">MDCT</a></li>
<li><a href="Psychoacoustics" title="Psychoacoustics">Psychoacoustic model</a></li></ul>
</div></td></tr></tbody></table><div></div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Image_compression" title="Image compression">Image</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"></div><table class="nowraplinks navbox-subgroup" style="border-spacing:0"><tbody><tr><th scope="row" class="navbox-group" style="width:1%">Concepts</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Chroma_subsampling" title="Chroma subsampling">Chroma subsampling</a></li>
<li><a href="Coding_tree_unit" title="Coding tree unit">Coding tree unit</a></li>
<li><a href="Color_space" title="Color space">Color space</a></li>
<li><a href="Compression_artifact" title="Compression artifact">Compression artifact</a></li>
<li><a href="Image_resolution" title="Image resolution">Image resolution</a></li>
<li><a href="Macroblock" title="Macroblock">Macroblock</a></li>
<li><a href="Pixel" title="Pixel">Pixel</a></li>
<li><a href="Peak_signal-to-noise_ratio" title="Peak signal-to-noise ratio">PSNR</a></li>
<li><a href="Quantization_(image_processing)" title="Quantization (image processing)">Quantization</a></li>
<li><a href="Standard_test_image" title="Standard test image">Standard test image</a></li>
<li><a href="Texture_compression" title="Texture compression">Texture compression</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Methods</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Chain_code" title="Chain code">Chain code</a></li>
<li><a href="Discrete_cosine_transform" title="Discrete cosine transform">DCT</a></li>
<li><a href="Deflate" title="Deflate">Deflate</a></li>
<li><a href="Fractal_compression" title="Fractal compression">Fractal</a></li>
<li><a href="Karhunen%E2%80%93Lo%C3%A8ve_theorem" class="mw-redirect" title="Karhunen–Loève theorem">KLT</a></li>
<li><a href="Pyramid_(image_processing)" title="Pyramid (image processing)">LP</a></li>
<li><a href="Run-length_encoding" title="Run-length encoding">RLE</a></li>
<li><a href="Wavelet_transform" title="Wavelet transform">Wavelet</a>
<ul><li><a href="Daubechies_wavelet" title="Daubechies wavelet">Daubechies</a></li>
<li><a href="Discrete_wavelet_transform" title="Discrete wavelet transform">DWT</a></li>
<li><a href="Embedded_zerotrees_of_wavelet_transforms" title="Embedded zerotrees of wavelet transforms">EZW</a></li>
<li><a href="Set_partitioning_in_hierarchical_trees" title="Set partitioning in hierarchical trees">SPIHT</a></li></ul></li></ul>
</div></td></tr></tbody></table><div></div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Data_compression#Video" title="Data compression">Video</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"></div><table class="nowraplinks navbox-subgroup" style="border-spacing:0"><tbody><tr><th scope="row" class="navbox-group" style="width:1%">Concepts</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Bit_rate" title="Bit rate">Bit rate</a>
<ul><li><a href="Average_bitrate" title="Average bitrate">ABR</a></li>
<li><a href="Constant_bitrate" title="Constant bitrate">CBR</a></li>
<li><a href="Variable_bitrate" title="Variable bitrate">VBR</a></li></ul></li>
<li><a href="Display_resolution" title="Display resolution">Display resolution</a></li>
<li><a href="Film_frame" title="Film frame">Frame</a></li>
<li><a href="Frame_rate" title="Frame rate">Frame rate</a></li>
<li><a href="Video_compression_picture_types" title="Video compression picture types">Frame types</a></li>
<li><a href="Interlaced_video" title="Interlaced video">Interlace</a></li>
<li><a href="Video#Characteristics_of_video_streams" title="Video">Video characteristics</a></li>
<li><a href="Video_quality" title="Video quality">Video quality</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Video_codec" title="Video codec">Codec</a><br>parts</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Discrete_cosine_transform" title="Discrete cosine transform">DCT</a></li>
<li><a href="Differential_pulse-code_modulation" title="Differential pulse-code modulation">DPCM</a></li>
<li><a href="Deblocking_filter" title="Deblocking filter">Deblocking filter</a></li>
<li><a href="Lapped_transform" title="Lapped transform">Lapped transform</a></li>
<li>Motion
<ul><li><a href="Motion_compensation" title="Motion compensation">Compensation</a></li>
<li><a href="Motion_estimation" title="Motion estimation">Estimation</a></li>
<li><a href="Motion_vector" class="mw-redirect" title="Motion vector">Vector</a></li></ul></li>
<li><a href="Wavelet_transform" title="Wavelet transform">Wavelet</a>
<ul><li><a href="Daubechies_wavelet" title="Daubechies wavelet">Daubechies</a></li>
<li><a href="Discrete_wavelet_transform" title="Discrete wavelet transform">DWT</a></li></ul></li></ul>
</div></td></tr></tbody></table><div></div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Information_theory" title="Information theory">Theory</a></th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Compressed_data_structure" title="Compressed data structure">Compressed data structures</a>
<ul><li><a href="Compressed_suffix_array" title="Compressed suffix array">Compressed suffix array</a></li>
<li><a href="FM-index" title="FM-index">FM-index</a></li></ul></li>
<li><a href="Entropy_(information_theory)" title="Entropy (information theory)">Entropy</a></li>
<li><a href="Information_theory" title="Information theory">Information theory</a>
<ul><li><a href="Timeline_of_information_theory" title="Timeline of information theory">Timeline</a></li></ul></li>
<li><a href="Kolmogorov_complexity" title="Kolmogorov complexity">Kolmogorov complexity</a></li>
<li><a href="Prefix_code" title="Prefix code">Prefix code</a></li>
<li><a href="Quantization_(signal_processing)" title="Quantization (signal processing)">Quantization</a></li>
<li><a href="Rate%E2%80%93distortion_theory" title="Rate–distortion theory">Rate–distortion</a></li>
<li><a href="Redundancy_(information_theory)" title="Redundancy (information theory)">Redundancy</a></li>
<li><a href="Data_compression_symmetry" title="Data compression symmetry">Symmetry</a></li>
<li><a href="Smallest_grammar_problem" title="Smallest grammar problem">Smallest grammar problem</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Community</th><td class="navbox-list-with-group navbox-list navbox-even" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Hutter_Prize" title="Hutter Prize">Hutter Prize</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">People</th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Mark_Adler" title="Mark Adler">Mark Adler</a></li>
<li><a href="Phil_Katz" title="Phil Katz">Phil Katz</a></li></ul>
</div></td></tr></tbody></table></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-06-24" href="https://en.wikipedia.org/wiki/?title=Huffman_coding&oldid=1297180088">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>